home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / lzhsourc.lzh / LZH.SRC / HUF.S < prev    next >
Text File  |  1992-07-02  |  7KB  |  220 lines

  1. N               equ 4096                ;Buffer size
  2. F               equ 60                  ;lookahead buffer size
  3. THRESHOLD       equ 2
  4. N_CHAR          equ 256-THRESHOLD+F     ;kinds of characters (0..N_CHAR-1)
  5. T               equ N_CHAR*2-1          ;size of table
  6.  
  7. ; -----------------------------------------------------------------------
  8. ;               putCode
  9. ; -----------------------------------------------------------------------
  10.  
  11. ; void Putcode(register int l, register int c)
  12.  
  13. ; d7 = putlen
  14. ; register a2 = putlen
  15. ;        -2(a2)=putbuf
  16.                 macro   putcode
  17.                 local PutCode1
  18.                 local PutCode2
  19.                 movem.w D3-D4,-(SP)
  20.                 move.w  D0,D4
  21.                 move.w  D1,D3
  22.                 move.b  d7,D2           ; d2=putlen
  23.                 lsr.w   D2,D1
  24.                 or.w    D1,d6           ; putbuf != c >> putlen
  25.  
  26.                 add.b   D0,d7           ; putlen+=l
  27.                 cmpi.b  #8,d7           ; putlen
  28.                 bcs     PutCode2
  29.  
  30.                 move.w  d6,D0           ; putbuf
  31.                 lsr.w   #8,D0
  32.                 fputc   d0              ; putc(putbuf,outfile)
  33. ; if ((putlen -= 8) >=8)
  34.                 subq.b  #8,d7           ; putlen
  35.                 cmpi.b  #8,d7           ; putlen
  36.                 bcs.b   PutCode1
  37.  
  38.                 fputc   d6              ; putc(putbuf,outfile)
  39.                 addq.l  #2,codesize     ; codesize+=2
  40.                 subq.b  #8,d7           ;putlen-=8
  41.  
  42.                 move.w  D3,D0
  43.                 move.b  D4,D1
  44.                 sub.b   d7,D1           ; putlen
  45.                 lsl.w   D1,D0
  46.                 move.w  D0,d6           ; putbuf = c << (l-putlen)
  47.                 bra.b   PutCode2
  48.  
  49. PutCode1:       move.w  d6,D0           ; putbuf
  50.                 lsl.w   #8,D0
  51.                 move.w  D0,d6           ; putbuf <<=8
  52.                 addq.l  #1,codesize     ; codesize++
  53. PutCode2:       movem.w (SP)+,D3-D4
  54.                 endm
  55.  
  56. ; -----------------------------------------------------------------------
  57. ;               update
  58. ; -----------------------------------------------------------------------
  59.  
  60.  
  61. ; void update(int c);
  62. ; register int i,j,k,l;
  63. ; register D3 = c
  64. ; register D1 = k
  65. ; register D2 = l
  66. ; register A1 = son
  67.  
  68. ; register D5 = cardinal c
  69. ; a4 = freq[c]
  70.  
  71. ;uses: d0,d1,d2,d5
  72. ;      a0,a1,a2,a3,a4,a6
  73. ; expects:   a2 = freq          d2 = 2*T
  74. ;            a4 = son
  75.                 macro   update
  76.                 local   upd_1
  77.                 local   upd_2
  78.                 local   upd_2a
  79.                 local   upd_2b
  80.                 local   upd_3
  81.                 local   upd_4
  82.                 local   upd_5
  83.                 local   upd_6
  84.                 local   updx
  85.                 local   updx1
  86.                 local   updrecon
  87.  
  88.                 tst.w   R*2(A2)  ; if freq[R] == MAX_FREQ
  89.                 bmi     updrecon
  90.  
  91. upd_1:          lea     prnt-freq(a2),A0         ; A0 = prnt
  92.  
  93.                 move.w  0(a0,d0.w),d0
  94. ; do {
  95.                 lea     0(A2,d0.w),A1   ; A1 = freq[c]
  96.                 addq.w  #1,(A1)         ; freq[c]++
  97.  
  98. ; Ab hier: l=d5
  99. ; if the order is disturbed, exchange nodes
  100.                 cmpm.w  (A1)+,(a1)+     ; if k>freq[l=c+1])
  101.                 bcs.b   upd_2b
  102.  
  103. upd_2a:         move.w  0(a0,d0.w),d0
  104.                 beq.b   updx
  105. ; do {
  106. upd_2:          lea     0(A2,d0.w),A1   ; A1 = freq[c]
  107.                 addq.w  #1,(A1)         ; freq[c]++
  108.  
  109. ; Ab hier: l=d5
  110. ; if the order is disturbed, exchange nodes
  111.                 cmpm.w  (A1)+,(a1)+     ; if k>freq[l=c+1])
  112.                 bcc.b   upd_2a
  113.  
  114. ; while k > freq[++l]
  115. upd_2b:         subq.w  #1,-4(a1)
  116.                 move.w  -4(a1),d1
  117. upd_3:          cmp.w   (a1)+,D1
  118.                 beq.b   upd_3           ; while (k>freq[++l]);
  119.                 subq.l  #4,a1
  120.                 addq.w  #1,(a1)
  121.  
  122.                 sub.l   A2,a1
  123.  
  124.                 move.w  0(a4,d0.w),d4   ; i=son[c]
  125.                 move.w  a1,(a0,d4.w)    ;prnt[i]=l
  126.  
  127.                 cmp.w   d2,d4         ; if i<T
  128.                 bge.b   upd_4
  129.                 move.w  a1,2(A0,d4.w)   ; prnt[i+1]=l
  130.  
  131. upd_4:          move.w  0(A4,a1.w),D1         ; j=son[l]
  132.                 move.w  d4,0(A4,a1.w)       ; son[l]=j
  133.  
  134.                 move.w  d0,(A0,d1.w)    ; prnt[j] = c
  135.  
  136.                 cmp.w   d2,D1         ; if j<T
  137.                 bge.b   upd_5
  138.                 move.w  d0,2(A0,d1.w)   ; prnt[j+1]=c
  139.  
  140. upd_5:          move.w  D1,0(a4,d0.w)   ; son[c]=j
  141.                 move.w  a1,d0
  142. upd_6:          move.w  0(a0,d0.w),d0
  143.                 beq.b   updx1
  144. ; do {
  145.                 lea     0(A2,d0.w),A1   ; A1 = freq[c]
  146.                 addq.w  #1,(A1)         ; freq[c]++
  147. ;                move.w  (A1)+,D1         ; k=freq[c]
  148.  
  149. ; Ab hier: l=d5
  150. ; if the order is disturbed, exchange nodes
  151.                 cmp.w   (A1)+,(a1)+     ; if k>freq[l=c+1])
  152.                 bcc.b   upd_6
  153.                 bra.b   upd_2b
  154. ; while k > freq[++l]
  155. updrecon:       bsr     reconst
  156.                 bra     upd_1
  157. updx1:
  158. updx:
  159.                 endm
  160.  
  161. ; void EncodeChar(unsigned c);
  162. ; register unsigned i;
  163. ; register int j,k;
  164. ; D5 = c
  165. ; D3 = i
  166. ; d4 = j
  167. ; d0 = k
  168.                 macro   EncodeCh
  169.                 local   Enchar1
  170.                 local   Enchar2
  171.                 local   Enchar3
  172.                 local   Enchar4
  173.                 local   Enchar5
  174.                 local   Enchar6
  175.                 move.w  d5,-(SP)
  176.                 move.w  #2*R,d2
  177.                 move.w  D0,D5           ; c
  178.                 moveq   #0,D1           ;i=0
  179.                 move.l  d1,a1           ;j=0
  180.                 moveq   #0,d4           ;shift=0
  181.                 lea     prnt-rson(a3),A0
  182.                 add.w   #T,D0           ; T
  183.                 add.w   D0,D0
  184.                 move.w  0(A0,D0.w),D0   ; k=prnt[c+T]
  185. ; while
  186. ; if (k & 1) i +=0x8000
  187. Enchar1:        addq.w  #1,d4
  188.                 btst    #1,D0
  189.                 beq.b   Enchar2
  190.                 lsr.w   d4,d1
  191.                 add.w   d4,a1
  192.                 moveq   #0,d4
  193.                 add.w   #$8000,d1
  194. Enchar2:        move.w  0(A0,D0.w),D0   ; k=prnt[k]
  195.                 cmp.w   d2,D0         ; R
  196.                 bne.b   Enchar1
  197.  
  198. ; putcode(j,i)
  199. Enchar5:        add.w   d4,a1
  200.                 move.w  a1,D0
  201.                 lsr.w   d4,d1
  202.                 lea     freq-rson(a3),a2
  203.                 putcode
  204. ; update(c)
  205.                 move.w  D5,D0
  206.                 add.w   D0,D0
  207.                 lea     freq-rson(a3),a2
  208.                 lea     son-rson(a3),a4
  209.                 move.w  #2*T,D2
  210.                 add.w   d2,d0
  211.                 update
  212.                 move.w  (SP)+,D5
  213.                 endm
  214.  
  215.  
  216. freq:           ds.w    T+1
  217. prnt:           ds.w    T+N_CHAR
  218. son:            ds.w    T
  219.  
  220.